package com.tenebrous.jestic.utils;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import lombok.Data;

import java.util.Date;

public class DateUtils {

    /**
     * 获取时间并比较
     * 传入一个固定时间，与当前时间进行比较
     *
     * @param requireDayStamp            要求计算的时间
     * @param requireDaynum             要求计算的天数
     * @return
     */
    public static DateResult getTimeAndCompareTo(Date requireDayStamp, int requireDaynum) {
        DateResult result = new DateResult();
        Date dt = new Date();
        Date beforeTime = DateUtil.offset(requireDayStamp, DateField.DAY_OF_MONTH, requireDaynum);
        if (beforeTime.compareTo(dt) > 0) {
            // 说明当前时间还未到beforeTime
            result.setBeforeFlag(false);
            result.setReserveFlag(false);
            result.setDayNum(getDayNum(requireDayStamp, dt));
        } else {
            result.setBeforeFlag(true);
            // 判断当前时间是否超过预定时间
            if (requireDayStamp.compareTo(dt) > 0) {
                result.setReserveFlag(false);
                result.setDayNum(getDayNum(requireDayStamp, dt));
            } else {
                result.setReserveFlag(true);
                result.setDayNum(getDayNum(dt, requireDayStamp));
            }
        }
        return result;
    }

    public static Boolean getTimeAndCompareTo(Date requireDayStamp, Date currentTime, int requireDaynum) {
        // 预定时间+天数，这个范围内是有效的，超过该天则无效
        Date afterTime = DateUtil.offset(requireDayStamp, DateField.DAY_OF_MONTH, requireDaynum);
        if (currentTime.compareTo(afterTime) > 0) {
            return true;
        }
        return false;
    }

    public static Long getDayNum(Date startDay, Date endDay) {
        return DateUtil.between(startDay, endDay, DateUnit.DAY);
    }

    @Data
    public static class DateResult {

        // 当前时间是否超过预定前N天时间
        private Boolean beforeFlag;

        // 当前时间是否超过预定时间
        private Boolean reserveFlag;

        // 当前时间与预定时间相差几天
        private Long dayNum;
    }

    public static void main(String[] args) {
        String dateStr = "2022-12-07 09:59:20";
        Date date = DateUtil.parse(dateStr, "yyyy-MM-dd HH:mm:ss");
        System.out.println(DateUtils.getTimeAndCompareTo(date, new Date(), 10));
    }


}
